#!/usr/bin/python
#encoding=utf-8

import re,urllib2,os,commands
import sys
from xml.dom import minidom
import getopt
import shutil


def help():
    print sys.argv[0], " -f file -d dir\n-h/--help for help"
    
def get_rpm(url, rpmname):
    """parsing url and get the rpm's url
    """
    content = urllib2.urlopen(url).read()
    
    
    web_rpmname = rpmname.replace('+', '\+')
    #web_rpmname = rpmname
    patten = '<a href="(' + web_rpmname + '.*\.rpm)">'
    rpm = re.findall(patten, content)
    if len(rpm) == 0:
        print "Error: can't find the rpm of ", web_rpmname, "in url of ", url
        notfind_rpm.append(rpmname)
        return 1
    elif len(rpm) > 1:
        print "Warning: There are more than one rpms of ", web_rpmname, "in url of ", url
    return rpm

def get_rpm_name(rpmdir):
    rpm = os.listdir(rpmdir)[0]
    
    (status, output) = commands.getstatusoutput('rpm -qp --nosignature --qf "%{name}" ' + rpmdir + '/' + rpm + ' 2> /dev/null')
    if status == 0:
        return output
    else:
        print "Error: get_rpm_name failed"
        exit(1)

def download_rpm(tmp_url, rpm_all, dir, rpmname):
    """parsing url and get the rpm's url
    """
    if len(rpm_all) == 1:
        rpm = rpm_all[0]
        url = tmp_url + '/' + rpm
        rpmpath = dir + '/' + rpm
        if os.path.isfile(rpmpath) == True:
            print "Error: The rpm of ", rpmpath, "has been downloaded"
            print "\tthere are maybe two same rpm of ", rpmpath, "in you config xml file. Please check it"
            exit(1)

        cmd = "wget " + url + " -P " + dir + " &> /dev/null "
        if os.system(cmd) != 0:
            print "run: ", cmd, "    failed"
            exit(1)
        else:
            print "Downloaded " + url + " to " + dir
    else:
        find_rpm = 0
        for i in range(0, len(rpm_all)):
            url = tmp_url + '/' + rpm_all[i]
            tmp_dir = dir + '_tmp'
            if os.path.isdir(dir) == False:
                os.mkdir(dir)
            if os.path.isdir(tmp_dir) == True:
                shutil.rmtree(tmp_dir,True)
            cmd = "wget " + url + " -P " + tmp_dir + " &> /dev/null "
            if os.system(cmd) != 0:
                print "run: ", cmd, "    failed"
                exit(1)
            
            if get_rpm_name(tmp_dir) == rpmname:
                rpm = rpm_all[i]
                url = tmp_url + '/' + rpm
                rpmpath = dir + '/' + rpm
                if os.path.isfile(rpmpath) == True:
                    print "Error: The rpm of ", rpmpath, "has been downloaded"
                    print "\tthere are maybe two same rpm of ", rpmpath, "in you config xml file. Please check it"
                    exit(1) 
                (status, output) = commands.getstatusoutput('cp ' + tmp_dir + '/' + rpm + ' ' + rpmpath)
                if status != 0:
                    print "Error: cp rpm failed"
                    exit(1)
                if os.path.isdir(tmp_dir) == True:
                    shutil.rmtree(tmp_dir,True)
                find_rpm = find_rpm + 1
        if find_rpm == 0:
            print "Error: can't find the rpm of ", rpmname, "in url of ", url
            exit(1)
        elif find_rpm >= 2:
            print "Error: There two or more rpm have the samename: ", rpmname
            exit(1)
        if os.path.isdir(tmp_dir) == True:
            shutil.rmtree(tmp_dir,True)

if len(sys.argv) < 4:
    help()
    exit(1)

try:
    options,args = getopt.getopt(sys.argv[1:],"f:d:p:h",["file=","dir=","ip=","help"])
except getopt.GetoptError:
    sys.exit(1)

ip = 'obsserver_ip'
for name,value in options:
    if name in ('-f', '--file'):
        repo_conf_file = value
    elif name in ('-d', '--dir'):
        repo_dir = value
    elif name in ('-p', '--ip'):
        ip = value
    else:
        help()
        exit(0)

#repo_conf_file = sys.argv[1]
#repo_dir = sys.argv[2]



if os.path.isfile(repo_conf_file) == False:
    print repo_conf_file, "is not a file"
    exit(1)

if os.path.isdir(repo_dir) == False:
    if os.mkdir(repo_dir) == False:
        print "mkdir ", repo_dir, " failed"
        exit(1)


notfind_rpm = []
try:  
    xmldoc = minidom.parse(repo_conf_file)  
except :  
    print "Error: Can't parse Xml File of %s.\n" % (repo_conf_file)
    exit(1) 
repo = xmldoc.getElementsByTagName('repo')
for i in range(0, len(repo)):
    repo_name = repo[i].attributes['name'].value
    repo_url = repo[i].attributes['url'].value
    repo_url = repo_url.replace("obsserver_ip", ip)
    #print i, repo_url
    for arch in repo[i].childNodes:
        if arch.nodeType == arch.ELEMENT_NODE:
            #print "xdf", node.attributes['type'].value
            if arch.nodeName == 'arch':
                arch_type = arch.attributes['type'].value
                tmp_url = repo_url + '/' + arch_type
                for rpm in arch.childNodes:
                    if rpm.nodeType == rpm.ELEMENT_NODE:
                        if rpm.nodeName == 'rpm':
                            rpmname = rpm.attributes['name'].value
                            rpm_all = []
                            rpm_all = get_rpm(tmp_url, rpmname)
                            if rpm_all == 1:
                                continue
                            download_rpm(tmp_url, rpm_all, repo_dir + '/' + repo_name, rpmname)
                            #print rpm.attributes['name'].value
                        else:
                            print "Error: unknown node Tag name of ", rpm.nodeName    
            else:
                print "Error: unknown node Tag name of ", node.nodeName

if len(notfind_rpm) > 0:
    print "Error: Not found rpm:", notfind_rpm
    exit(1)
        

